import _DataSet from "choerodon-ui/pro/lib/data-set";
import _filter from "lodash/filter";
import _map from "lodash/map";
import { OPERATOR_TYPE } from "../../constant";
/**
 * 字段改变事件
 * @param record
 * @param name
 * @param value
 * @param fieldArr
 */

function handleFieldChange(_ref, fieldArr) {
  var record = _ref.record,
      name = _ref.name,
      value = _ref.value;

  if (name === 'fieldName') {
    record.set('comparator', '');

    _map(fieldArr, function (field) {
      if (value === field.value) {
        record.getField('comparator').set('options', new _DataSet({
          selection: 'single',
          data: _filter(OPERATOR_TYPE[field.type.toUpperCase()], function (type) {
            return type.value.indexOf('NULL') === -1;
          })
        }));
      }
    });
  }
}

export default (function (fieldArr) {
  return {
    paging: false,
    fields: [{
      name: 'fieldName',
      type: 'string',
      label: '字段',
      unique: 'group',
      options: new _DataSet({
        selection: 'single',
        paging: false,
        data: fieldArr
      }),
      required: true
    }, {
      name: 'comparator',
      type: 'string',
      label: '比较符',
      unique: 'group',
      required: true,
      options: new _DataSet({
        selection: 'single',
        paging: false,
        data: OPERATOR_TYPE.ALL
      })
    }, {
      name: 'searchQueryId',
      type: 'number'
    }],
    events: {
      update: function update(props) {
        return handleFieldChange(props, fieldArr);
      }
    }
  };
});